package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.StudentObserve;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.query.StudentObserveQueryObject;
import cn.wolfcode.crm.service.IStudentObserveService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


;import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("studentObserve")
public class StudentObserveController {
    @Autowired
    private IStudentObserveService studentObserveService;

    @RequiresPermissions(value = {"studentObserve:view", "升班留班查看"}, logical = Logical.OR)
    @RequestMapping("view")
    public String view() {
        return "studentObserve/studentObserve";
    }

    @RequiresPermissions(value = {"studentObserve:query", "升班留班列表"}, logical = Logical.OR)
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(StudentObserveQueryObject qo) {
        return studentObserveService.quey4List(qo);
    }

    @RequiresPermissions(value = {"studentObserve:saveOrUpdate", "升班留班新增/编辑"}, logical = Logical.OR)
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(StudentObserve entity) {
        try {
            studentObserveService.saveOrUpdate(entity);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("操作失败!原因:" + e.getMessage());
        }
    }


    @RequestMapping("exportXls")
    @ResponseBody
    public void exportXls(HttpServletResponse response, StudentObserveQueryObject qo) throws Exception {

        //设置文件下载响应头
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        Workbook book = studentObserveService.exportXls(qo);
        //写入数据输出到浏览器
        book.write(response.getOutputStream());
    }

    @RequestMapping("changeState")
    @ResponseBody
    public JsonResult changeState(Long id,Long newClass){
        System.out.println(newClass);
        JsonResult result = new JsonResult();
        try{
            studentObserveService.changeState(id,newClass);
        }catch (Exception e){
            result.mark(e.getMessage());
            return result;
        }
        return result;
    }

    @RequestMapping("unchangeState")
    @ResponseBody
    public JsonResult unchangeState(Long id,Long oldClass){
        JsonResult result = new JsonResult();
        try{
            studentObserveService.unchangeState(id,oldClass);
        }catch (Exception e){
            result.mark(e.getMessage());
            return result;
        }
        return result;
    }


}
